package com.arnold.learn.couchbase;

import com.couchbase.client.java.CouchbaseCluster;
import com.couchbase.client.java.env.CouchbaseEnvironment;
import com.couchbase.client.java.env.DefaultCouchbaseEnvironment;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import java.util.concurrent.TimeUnit;

/**
 * Created by arnold.zhu on 2017/7/27.
 */
public class CouchbaseFactory {
    private static Logger logger = LoggerFactory.getLogger(CouchbaseFactory.class);

    private static ResourceBundle rb = ResourceBundle.getBundle("couchbase");
    private static String nodeList = rb.getString("couchbase.nodeList");

    private static CouchbaseCluster createCouchbaseCluster() {
        List<String> nodes = new ArrayList<>();
        String[] list = nodeList.split(",");
        for (String node : list) {
            logger.info(">>>>>>>>>>>>>>> Node list added: {}", node);
            nodes.add(node);
        }
        //Disable bootstrapCarrier, Enable bootstrapHttp,  set your http_port
        CouchbaseEnvironment env = DefaultCouchbaseEnvironment.builder()
                .bootstrapCarrierEnabled(false)
                .bootstrapHttpDirectPort(8091)
                .socketConnectTimeout((int) TimeUnit.SECONDS.toMillis(30))
                .connectTimeout(TimeUnit.SECONDS.toMillis(30)).build();
        // Create a cluster reference
        return CouchbaseCluster.create(env, nodeList);
    }

    public static CouchbaseCluster getCouchbaseCluster() {
        return createCouchbaseCluster();
    }
}
